package com.lindroy.androidplayer.base.adapter

import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DiffUtil
import androidx.viewpager2.adapter.FragmentStateAdapter

/**
 * @author Lin
 * @date 2022/1/22
 * @function 基类TabLayout的Adapter
 */
abstract class BaseTabAdapter<T : Any>(fragment: Fragment) : FragmentStateAdapter(fragment) {

    private val tabList = arrayListOf<T>()

    fun setData(list: List<T>) {
        DiffUtil.calculateDiff(object : DiffUtil.Callback() {
            override fun getOldListSize(): Int = tabList.size

            override fun getNewListSize(): Int = list.size

            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                tabList[oldItemPosition] == list[newItemPosition]

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                tabList[oldItemPosition] == list[newItemPosition]

        }).dispatchUpdatesTo(this)
        tabList.clear()
        tabList.addAll(list)
    }

    override fun getItemCount(): Int = tabList.size

    override fun createFragment(position: Int): Fragment = createItemFragment(tabList[position])

    abstract fun createItemFragment(item: T): Fragment

    /* override fun getItemId(position: Int) = projectIdList[position].toLong()

    override fun containsItem(itemId: Long): Boolean {
        XLog.d("itemId = $itemId,contains = ${createdIds.contains(itemId)}")
        return createdIds.contains(itemId)
    }*/
}